package com.atguigu.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.Exception.NotStockException;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.dao.WareInfoDao;
import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareInfoEntity;
import com.atguigu.gulimall.ware.feign.MemberFeignService;
import com.atguigu.gulimall.ware.service.WareInfoService;
import com.atguigu.gulimall.ware.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;
    @Resource
    private WareSkuDao wareSkuDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.eq("id", key).or()
                    .eq("name", key).or()
                    .eq("address", key).or()
                    .eq("areacode", key);
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo fare(Long addrId) {
        R info = memberFeignService.info(addrId);
        MemberAddressVo memberAddressVo = info.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
        });
        String phone = memberAddressVo.getPhone();
        String substring = phone.substring(phone.length() - 1);
        BigDecimal decimal = new BigDecimal(substring);
        FareVo fareVo = new FareVo();
        fareVo.setFare(decimal);
        fareVo.setMemberAddressVo(memberAddressVo);
        return fareVo;
    }

    @Transactional
    @Override
    public Boolean orderLockStock(WareSkuLockVo wareSkuLockVo) {
        List<OrderItemVo> locks = wareSkuLockVo.getLocks();
        List<SkuWareHasStockVo> collect = locks.stream().map(orderItemVo -> {
            SkuWareHasStockVo skuWareHasStockVo = new SkuWareHasStockVo();
            skuWareHasStockVo.setSkuId(orderItemVo.getSkuId());
            skuWareHasStockVo.setNum(orderItemVo.getCount());
            List<Long> wareIds = wareSkuDao.listWareIdsHasSkuStock(orderItemVo.getSkuId());
            skuWareHasStockVo.setWareId(wareIds);
//            skuWareHasStockVo.setWareId();
            return skuWareHasStockVo;
        }).collect(Collectors.toList());

        //锁库存
        for (SkuWareHasStockVo skuWareHasStockVo : collect) {
            boolean skuStock = false;
            List<Long> wareId = skuWareHasStockVo.getWareId();
            Long skuId = skuWareHasStockVo.getSkuId();
            Integer num = skuWareHasStockVo.getNum();
            if (wareId == null && wareId.size() > 0) {
                throw new NotStockException(skuId);
            }
            for (Long aLong : wareId) {
                Integer count = wareSkuDao.lockSkuStock(skuId, aLong, num);
                if (count == 1) {
                    skuStock = true;
                    break;
                }
            }
            if(!skuStock){
                throw new NotStockException(skuId);
            }
        }
        return true;
    }

}